package com.serie402.business.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.kiss.fw.dto.AbstractDAODTO;
import com.kiss.fw.exceptions.DAOException;
import com.kiss.fw.utils.Util;
import com.serie402.business.dao.dto.NewsDAODTO;
import com.serie402.business.dao.mappers.NewsMapper;
import com.serie402.common.bo.News;
import com.serie402.common.utils.Constants;

public final class NewsDAO extends Serie402DAO {
	private static final Logger logger = Logger.getLogger(NewsDAO.class);
	private static final NewsDAO instance = new NewsDAO();
	
	private NewsDAO() {
	}
	
	private static NewsDAODTO getNewsDAODTO(AbstractDAODTO _dto) {
		return (NewsDAODTO)_dto;
	}
	
	public static NewsDAO getInstance() {
		return instance;
	}
	
	@Override
	public AbstractDAODTO create(AbstractDAODTO _dto) throws DAOException {
		NewsDAODTO dto = getNewsDAODTO(_dto);
		
		SqlSession sqlSession = openSqlSession();
		
		if(sqlSession != null) {
			NewsMapper newsMapper = MapperFactory.getNewsMapper(sqlSession);
			
			try {
				newsMapper.createArticle(dto);
			}
			catch(Exception _exception) {
				handleException(logger, "Error creating news article", _exception);
			}
		}
		
		return dto;
	}
	
	@Override
	public AbstractDAODTO read(AbstractDAODTO _dto) throws DAOException {
		NewsDAODTO dto = (NewsDAODTO)_dto;
		SqlSession sqlSession = openSqlSession();
		
		if(sqlSession != null) {
			NewsMapper newsMapper = MapperFactory.getNewsMapper(sqlSession);
			List<News> retrievedNewsList = null;
			
			// retrieve article
			if(dto.getNewsId() > 0) {
				News retrievedArticle = null;
				
				try {
					retrievedArticle = newsMapper.retrieveArticle(dto);
				}
				catch(Exception _exception) {
					handleException(logger, "Error retrieving news article", _exception);
				}
				
				if(retrievedArticle != null) {
					retrievedNewsList = new ArrayList<News>(1);
					retrievedNewsList.add(retrievedArticle);
				}
			}
			
			// retrieve by category
			else if(!Util.isStringEmpty(dto.getCategory()) && Util.isStringEmpty(dto.getSubCategory())) {
				try {
					retrievedNewsList = newsMapper.retrieveArchivedNewsByCategory(dto);
				}
				catch(Exception _exception) {
					handleException(logger, "Error retrieving news by category", _exception);
				}
			}
			
			// retrieve by category and sub-category
			else if(!Util.isStringEmpty(dto.getCategory()) && !Util.isStringEmpty(dto.getSubCategory())) {
				try {
					retrievedNewsList = newsMapper.retrieveArchivedNewsByCategoryAndSubCategory(dto);
				}
				catch(Exception _exception) {
					handleException(logger, "Error retrieving news by category & sub-category", _exception);
				}
			}
			
			// updating dto with the results
			dto.setNews(retrievedNewsList);
		}
		
		News n = new News();
		News n1 = new News();
		
		n.setImportance(Constants.Misc.NEWS_IMPORTANCE_BREAKING);
		n1.setImportance(Constants.Misc.NEWS_IMPORTANCE_HOT);
		
		n.setTitle("Breaking");
		n1.setTitle("This is the title");
		
		n.setNewsId(1);
		n1.setNewsId(2);
		
		n.setDescription("breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking breaking ");
		n1.setDescription("description1 description1 description1 description1 description1 description1");
		
		List<News> newsList = null;
		
		if(dto.getNewsId() > 0) {
			newsList = new ArrayList<News>(1);
			newsList.add(n1);
		}
		else {
			newsList = new ArrayList<News>(25);
			
			newsList.add(n);
			newsList.add(n1);
			newsList.add(n1);
			
			newsList.add(n1);
			
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
			newsList.add(n1);
		}
		
		dto.setNews(newsList);
		
		return _dto;
	}
	
	@Override
	public AbstractDAODTO update(AbstractDAODTO _dto) throws DAOException  {
		NewsDAODTO dto = getNewsDAODTO(_dto);
		
		return dto;
	}
	
	@Override
	public AbstractDAODTO delete(AbstractDAODTO _dto) throws DAOException  {
		NewsDAODTO dto = getNewsDAODTO(_dto);
		
		return dto;
	}

	@Override
	public AbstractDAODTO search(AbstractDAODTO _dto) throws DAOException {
		return _dto;
	}
}